package com.core.util;

import java.io.File;

import java.lang.reflect.Field;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import com.dsp.bean.InsureResult;

/**
 * 常用方法
 * 
 * @author laker
 * @version 1.0 18/8/11
 */
public class DspUtil {
	/** 　Excel中的日期與DB中的日期轉換 */
	public static final String[] EN_MONTH = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
			"Dec" };
	public static final String[] CN_MONTH = new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };

	public static String Log4J = "DSP";
	
	private static String realPath = null;
	
	private static SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 得到in的查询条件
	 * @param value
	 * @return
	 */
	public static String getSQLIn(List<? extends Object> value){
		StringBuffer sb = new StringBuffer(50);
		for(Object obj : value){
			if(obj instanceof Long || obj instanceof Integer){
				sb.append(",").append(Long.valueOf(obj.toString()));
			} else {
				sb.append(",'").append(obj.toString()).append("'");
			}
		}
		return sb.substring(1);
	}
	
	/**
	 * 将字符串数组转换成Long数组
	 * @param objStr
	 * @return
	 * @throws ClassCastException 
	 */
	public static Long[] arrayToLong(String[] objStr) {
		Long[] newLong = new Long[objStr.length];
		int i = 0 ;
		for(String str : objStr){
			newLong[i++] = Long.valueOf(str);
		}
		return newLong;
	}
	
	/**
	 *得到系统当前年月
	 * @return
	 */
	public static String getCurrentYearMonth(){
		Date dd = new Date(System.currentTimeMillis());
		String str = dd.toString();
		str = str.replaceAll("[-]", "");
		return str.substring(0,6);
	}
	/**
	 * 系统当前日期
	 * @return
	 */
	public static String getCurrentDate(){
		Date dd = new Date(System.currentTimeMillis());
		return dd.toString();
	}
	/**
	 * 系统当前日期的下一年日期
	 * @return
	 */
	public static java.sql.Date getNextYearDate(){
		java.util.Date curDate = new java.util.Date();
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(curDate);
		int year = cal.get(GregorianCalendar.YEAR) + 1;
		int month= cal.get(GregorianCalendar.MONTH)+1;
		int day  = cal.get(GregorianCalendar.DATE);
		try {
			return  new Date(sqlFormat.parse(year + "-" + month +"-" + day).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	/**
	 * 通过当前年月，得到月份的第一天和最后一天
	 * @return
	 */
	public static java.sql.Date[] getFirstAndLastByMonth(String yearMonth){
		String year = yearMonth.substring(0, 4);
		String month= yearMonth.substring(4,6);
		Calendar cal = Calendar.getInstance();
		cal.set(cal.YEAR , reqInteger(year));
		cal.set(cal.MONTH, reqInteger(month));
		int lastDay = cal.getActualMaximum(cal.DAY_OF_MONTH);
		return new java.sql.Date[]{getDateFromStr(year+"-"+month+"-01"),getDateFromStr(year+"-"+month+"-"+lastDay)};
	}
	
	/**
	 * 单笔
	 * @param aaa
	 * @return
	 */
	public static boolean hasOnlyOne(String[] aaa){
		return aaa.length == 1;
	}
	
	public static void setRealPath(String path){
		realPath = path;
	}
	
	public static String getRealPath(){
		return realPath;
	}
	/**
	 * 下载文件
	 * @return
	 */
	public static String getDownloadsPath(){
		return getRealPath() + File.separator+"downloads" + File.separator;//request.getParameter("PAFName");
	}

	/**
	 * 得到上下文
	 * 
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		return DspContext.getBean(beanName);
	}

	/**
	 * 判断list是否为空
	 * 
	 * @param l
	 * @return
	 */
	public static boolean isEmptyList(List<? extends Object> l) {
		return (l == null || l.size() == 0);
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(String obj) {
		return (obj == null || "".equals(obj));
	}

	public static java.sql.Date getDateFromStr(String timeStr) {
		try {
			return new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(timeStr).getTime());
		} catch (Exception e) {
			return new java.sql.Date(0);
		}
	}
	
	/**
	 * 替换值
	 * 
	 * @param obj
	 * @param val
	 * @return
	 */
	public static String req(String obj, String val) {
		if (isEmpty(obj))
			return val;
		return obj;
	}
	
	public static String req(Object obj , String val){
		if (obj == null) return val;
		return req(obj.toString() , val);
	}

	public static Integer reqInteger(Object obj){
		if (obj == null || isEmpty(obj.toString())) return 0;
		return Integer.parseInt(obj.toString());
	}
	
	public static Long reqLong(Object obj){
		if (obj == null || isEmpty(obj.toString())) return 0L;
		return Long.valueOf(obj.toString());
	}
	
	/**
	 * 
	 * @param obj
	 * @param val
	 * @return
	 */
	public static Date reqDate(Object obj , Date val){
		if (obj == null || isEmpty(obj.toString())) return val;
		return getDateFromStr(obj.toString());
	}
	
	/**
	 * 　從Excel中取出的日期與數據庫中標準的日期不一樣，故進行轉換
	 * 
	 * @param args0
	 *            [Sun Nov 01 08:00:00 CST 2009] -->2009-11-01
	 * @return
	 */
	public static String getCellDate(String args0) {
		String rtn = "";
		if (args0 != null && args0.equals("") == false) {
			String[] tmp = args0.split(" ");
			if (tmp.length == 6) {
				rtn = tmp[5] + "-" + EnToCn(tmp[1]) + "-" + tmp[2];
			}
		}
		return rtn;
	}

	/**
	 * 英文月份轉換成數字
	 * 
	 * @param args0
	 * @return
	 */
	public static String EnToCn(String args0) {
		String rtn = "";
		for (int i = 0; i < EN_MONTH.length; i++) {
			if (EN_MONTH[i].equals(args0)) {
				rtn = CN_MONTH[i];
				break;
			}
		}
		return rtn;
	}
	/**
	 * 验证格式是否为xls
	 * @param path
	 * @return
	 */
	public static boolean isExl(String path){
		if(path.substring(path.lastIndexOf(".") + 1).equalsIgnoreCase("xls") ||
			path.substring(path.lastIndexOf(".") + 1).equalsIgnoreCase("csv") ||
			path.substring(path.lastIndexOf(".") + 1).equalsIgnoreCase("xlsx")){
			return true;
		}
		return false;
	}

	/**
	 * java转换Unicode
	 * @param gbString
	 * @return
	 */
	public static String gbEncoding(final String gbString) {  
        char[] utfBytes = gbString.toCharArray();  
        String unicodeBytes = "";  
        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {  
            String hexB = Integer.toHexString(utfBytes[byteIndex]);  
            if (hexB.length() <= 2) {  
                hexB = "00" + hexB;  
            }  
            unicodeBytes = unicodeBytes + "\\u" + hexB;  
        }  
        System.out.println("unicodeBytes is: " + unicodeBytes);  
        return unicodeBytes;  
    }  
	
	public static void main(String[] args) throws ParseException {
		System.out.println(Date.valueOf("11-09-01"));
		System.out.println(getDateFromStr("11-09-01"));
		
		System.out.println(getNextYearDate());
		
		System.out.println(getCurrentYearMonth());
		
		System.out.println(Float.MIN_NORMAL);
		float a = Float.MIN_NORMAL;

		System.out.println(a == Float.MIN_NORMAL);
		List<String> one = new ArrayList<String>();
		one.add("a");
		one.add("d");
		List<String> two = new ArrayList<String>();
		two.add("c");
		two.add("b");
		one.addAll(two);
		System.out.println(one.size());
		for (String str : one) {
			System.out.println(str);
		}
		String tt = "110100    市辖区";
		System.out.println(tt.split(" {1,}").length);
		System.out.println("110000:" + "110000".matches("[0-9]{2}0000"));
		System.out.println("120000:" + "120000".matches("[0-9]{2}0000"));
		System.out.println("110100:" + "110100".matches("[0-9]{2}0000"));
		
		Random rand = new Random();
		for (int i = 0 ; i < 20 ; i++){
			System.out.println(rand.nextInt(20));
		}
		for (int i = 0 ; i < 20 ; i++){
			System.out.println(rand.nextInt(20));
		}
		
		String v = "a,b.c,e,f,...g";
		System.out.println(v.split("[,]").length);
		
		Class cls = InsureResult.class;
		Field[] field = cls.getDeclaredFields();
		try {
			Field ff = cls.getField("empName");
			System.out.println(ff.getName());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		for(Field f : field){
			Object obj = f.getType();
			if(obj instanceof java.lang.String){
				System.out.println("String");
			} else if (obj instanceof Float){
				System.out.println("Float");
			}
			
		}
		 
	}

}
